<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use voku\helper\HtmlDomParser;


class FoodsDataCatch2 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'food_data_catch2';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->scrawl_list();
        #$this->scraw_ips2();
    }

    private function scrawl_category() {
        $client = new Client();
        $response = $client->request('POST','https://food.hiyd.com/');
        $page = $response->getBody()->getContents();
        //var_dump($page);
        $home_html =  HtmlDomParser::str_get_html($page);
        $cart_list = $home_html->find('div[class=box-bd]')[0]->find('ul')->find('li');
        foreach ($cart_list as $key=>$cart) {
            $img = $cart->findOne('.img-wrap')->findOne('img')->getAttribute('src');
            $link = $cart->findOne('a')->getAttribute('href');
            $title = $cart->findOne('h3')->innertext;
            $desc = trim($cart->findOne('.eg_foods')->innertext);

            $img_content = $client->request('get',$img)->getBody()->getContents();
            $img_local = 'download/'.time().rand(0,999).'.png';
            Storage::disk('public')->put($img_local,$img_content);
            $img_local = '/'.$img_local;
            //var_dump(compact('img','link','title','desc','img_local'));
            \DB::table('foods_categories')->insert([
                'category_name'=>$title,
                'desc'=>$desc,
                'img_remote'=>$img,
                'img_local'=>$img_local,
                'link'=>$link,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
        }
    }

    private function scrawl_list() {
        $client = new Client();
        $cates = \DB::table('foods_categories')->get();
        $ips = ['101.83.33.2','103.19.2.10','102.199.21.10','193.122.21.10','122.19.12.12','123.199.222.110','118.129.22.11','115.43.22.5','153.199.32.17',];
        $user_agent_list=['Mozilla/5.0(Windows;U;WindowsNT6.1;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/5.1Safari/534.50','Mozilla/5.0(Macintosh;IntelMacOSX10.6;rv:2.0.1)Gecko/20100101Firefox/4.0.1','Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'];

        foreach ($cates as $cate) {
           /* $ip_info = \DB::table('ips')->orderBy(\DB::raw('RAND()'))->first();
            $ip_port_host = str_replace('http://','',$ip_info->ip);
            $ip_arr = explode(':',$ip_port_host);
            list($host,$port) =$ip_arr;*/

           $response= $client->request('get',$cate->link,[/*'curl'  => [
                   CURLOPT_PROXY => $host,
                   CURLOPT_PROXYPORT => $port,
                    CURLOPT_PROXYTYPE=> CURLPROXY_HTTP,
                    CURLOPT_PROXYAUTH=> CURLAUTH_BASIC
      ],*/'header'=>['user-agent'=>array_rand($user_agent_list),'Cookie'=>'_webp=1; Hm_lvt_27e0b89fc1f2e9d960a18d82d5f98dbd=1599616974,1599617593; Hm_lpvt_27e0b89fc1f2e9d960a18d82d5f98dbd=1599634935']])->getBody()->getContents();
           $first_page = HtmlDomParser::str_get_html($response);
           $page_size = $first_page->findOne('.mod-page')->find('a:nth-last-child(3)')->innertext;
           //var_dump($page_size);
           $li_list = $first_page->findOne('.box-bd')->findOne('ul')->find('li');
           foreach ($li_list as $key=>$li) {
               $alink = 'https:'.$li->findOne('a')->getAttribute('href');
               $this->scraw_detail($cate->id,$alink);
               break;
           }
           for ($i=2;$i<=$page_size;$i++) {
               $ip_info = \DB::table('ips')->orderBy(\DB::raw('RAND()'))->first();
               $ip_port_host = str_replace('http://','',$ip_info->ip);
               $ip_arr = explode(':',$ip_port_host);
               list($host,$port) =$ip_arr;
               $response= $client->request('get',$cate->link.'?page='.$i,[/*'curl'  => [
                   CURLOPT_PROXY => $host,
                   CURLOPT_PROXYPORT => $port,
                   CURLOPT_PROXYTYPE=> CURLPROXY_HTTP,
                   CURLOPT_PROXYAUTH=> CURLAUTH_BASIC
               ],*/'header'=>['user-agent'=>array_rand($user_agent_list),'X-Forwarded-For'=>array_rand($ips),'Cookie'=>'_webp=1; Hm_lvt_27e0b89fc1f2e9d960a18d82d5f98dbd=1599616974,1599617593; Hm_lpvt_27e0b89fc1f2e9d960a18d82d5f98dbd=1599634935']])->getBody()->getContents();
               $page = HtmlDomParser::str_get_html($response);
               $li_list = $page->findOne('.box-bd')->findOne('ul')->find('li');
               foreach ($li_list as $key=>$li) {
                   $alink = 'https:'.$li->findOne('a')->getAttribute('href');
                   $this->scraw_detail($cate->id,$alink);
               }
               //sleep(2);
               if($i%2==0) {
                   sleep(60);
               }
               break;
           }
           break;
        }
    }

    public function scraw_detail($cid,$link,$proxy=null) {
        var_dump($cid);
        var_dump($link);
        $client = new Client();
        $response= $client->request('get',$link,[/*'curl'  => [
            CURLOPT_PROXY => $proxy['host'],
            CURLOPT_PROXYPORT => $proxy['port'],
            CURLOPT_PROXYTYPE=> CURLPROXY_HTTP,
            CURLOPT_PROXYAUTH=> CURLAUTH_BASIC
        ],*/])->getBody()->getContents();
        $page = HtmlDomParser::str_get_html($response);
        $img = $page->findOne('.details-main')->findOne('.box-bd')->findOne('.img-wrap')->findOne('img')->getAttribute('src');
        $nutric_wrap = $page->findOne('.details-main')->findOne('.box-bd');
        $lis = $nutric_wrap->find('li');
        $calorie = $lis[0]->findOne('p')->innertext;
        $protein = $lis[1]->findOne('p')->plaintext;
        $carbohydrate = $lis[2]->findOne('p')->innertext;
        $fat = $lis[3]->findOne('p')->innertext;

        var_dump($calorie);
var_dump($protein);
var_dump($carbohydrate);
var_dump($fat);

    }

    public function scraw_ips() {
        $client = new Client();
        for ($i=1;$i<5;$i++) {
            try{
                $response = $client->request('get','https://www.kuaidaili.com/free/intr/'.$i)->getBody()->getContents();
                $page = HtmlDomParser::str_get_html($response);
                $trs = $page->findOne('table tbody')->find('tr');
                foreach ($trs as $tr){
                    $tds = $tr->find('td');
                    var_dump($tds[5]->innertext);
                    $speed = str_replace('秒','',$tds[5]->innertext);
                    var_dump($speed);
                    if($speed<1){
                        $host = $tds[0]->innertext;
                        $port = $tds[1]->innertext;
                        try{
                            $sock = fsockopen($host,$port,$error_no,$err_str,2);
                            //socket_set_timeout($sock,2,0);
                            var_dump($host);
                            var_dump($port);
                        }catch (\Exception $e) {
                            var_dump($e->getMessage());
                        };

                    }

                }
            }catch (\Exception $e) {
                var_dump($e->getMessage());
            }

        }

    }
    public function scraw_ips2() {
        $client = new Client();
        for ($i=1;$i<100;$i++) {
            try{
                $response = $client->request('get','https://www.89ip.cn/index_'.$i.'.html')->getBody()->getContents();
                $page = HtmlDomParser::str_get_html($response);
                $trs = $page->findOne('table tbody')->find('tr');
                foreach ($trs as $tr){
                    $tds = $tr->find('td');
                    //var_dump($tds[5]->innertext);
                   // $speed = str_replace('秒','',$tds[5]->innertext);
                   // var_dump($speed);
                    //if($speed<1){
                        $host = $tds[0]->innertext;
                        $port = $tds[1]->innertext;
                        try{
                            $sock = fsockopen($host,$port,$error_no,$err_str,2);

                            //socket_set_timeout($sock,2,0);
                            \Redis::sadd('ip_tables','http://'.$host.':'.$port);
                            \DB::table('ips')->insert(['ip'=>'http://'.$host.':'.$port]);
                            var_dump($host);
                            var_dump($port);
                        }catch (\Exception $e) {
                            var_dump($e->getMessage());
                        };

                    //}

                }
            }catch (\Exception $e) {
                var_dump($e->getMessage());
            }

        }

    }

}
